﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using csharp_webapi_415.Data;
using System.Web.Http.Description;
using csharp_webapi_415.Data.Models;

namespace csharp_webapi_415.Controllers
{
    public class MovieController : ApiController
    {

        // for webapi use IHttpActionResult vs ActionResult


        
        [Route("api/allmovies")]
        [ResponseType(typeof(Flick))]
        /// <summary>
        /// Fetch all movies in webiste
        /// </summary>
        /// <returns>Flick object</returns>
        public IHttpActionResult GetAllMovies()
        {
            var myArray = new string[] {"ricky", "bobby", "johnyDep"};
            using (ApplicationDbContext myDb = new ApplicationDbContext())
            {
                var myMovies = myDb.Flicks.ToList();
                // return View();
                return Ok(myMovies); // returns success
            } 
        }

        // get all actors...
        [Route("api/allactors")]
        public IHttpActionResult GetAllActors()
        {
            using (ApplicationDbContext myDb = new ApplicationDbContext())
            {
                var myActors = myDb.Actors.ToList();
                return Ok(myActors);   
            }
        }

        // get and Single actor by id
        [Route("api/actor/{aid}")]
        public IHttpActionResult GetActor(int aid)
        {
            using (ApplicationDbContext myDb = new ApplicationDbContext())
            {
                var actr = myDb.Actors.FirstOrDefault(a => a.ActorId == aid);
                return Ok(actr);
            }
            

        }

        // Delete a single actor
        [Route("api/delactor/{aid}")]
        public IHttpActionResult DeleteActor(int aid)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var delActor = db.Actors.FirstOrDefault(a => a.ActorId == aid);
                db.Actors.Remove(delActor);
                db.SaveChanges();
                return Ok(delActor);
            }
        }


    }
}